package com.example.autooperation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.autooperation.entity.Step;
import com.example.autooperation.service.StepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/step")
public class StepController {

    @Autowired
    private StepService stepService;

    @GetMapping
    public List<Step> listStepsByTaskId(@RequestParam int taskId) {
        QueryWrapper<Step> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId).orderByAsc("step_order");
        return stepService.list(queryWrapper);
    }

    @PostMapping
    public Step createStep(@Valid @RequestBody Step step) {
        stepService.save(step);
        return step;
    }

    @PutMapping("/{id}")
    public Step updateStep(@PathVariable int id, @Valid @RequestBody Step step) {
        step.setId(id);
        stepService.updateById(step);
        return step;
    }

    @DeleteMapping("/{id}")
    public void deleteStep(@PathVariable int id) {
        stepService.removeById(id);
    }
} 